package com.itheima.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.*;

import com.aliyun.teaopenapi.models.*;

import com.aliyun.teautil.models.*;

public class SMSUtils {
//     //发短信
//    public static void sendMessage(String number ,String code) {
//        DefaultProfile profile = DefaultProfile.getProfile("cd-creazycat", "LTAI5tB8JWGXKPi6cfNve8tN", "4mlqlbsY9FdF6UkPE1461cDSZ1knHv");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        SendSmsRequest request = new SendSmsRequest();
//        request.setSysRegionId("cd-creazycat");
//        request.setPhoneNumbers(number);
//        System.out.println("手机号码为:    " +number);
//        request.setSignName("阿里云短信测试");
//        request.setTemplateCode("SMS_154950909");
//        request.setTemplateParam("{\"code\":\""+code+"\"}");
//        try {
//            SendSmsResponse response = client.getAcsResponse(request);
//            System.out.println("短信发送成功");
//        }catch (ClientException e) {
//            e.printStackTrace();
//        }


    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static void sendMessage(String phone,String code) throws Exception {
//        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5tB8JWGXKPi6cfNve8tN", "4mlqlbsY9FdF6UkPE1461cDSZ1knHv");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                //"{\"phone\":"+  "{\"code\":"+
                .setPhoneNumbers(phone)
                .setTemplateParam(code);
        RuntimeOptions runtime = new RuntimeOptions();
        System.out.println("手机号码为:  " +sendSmsRequest.getPhoneNumbers());
        System.out.println("验证码为：   " + sendSmsRequest.getTemplateParam());
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        SendSmsResponse response = client.sendSms(sendSmsRequest);
        System.out.println(response.getBody());
    }

}